package com.yylending.las.lending.quota.idcard;

import android.os.Parcel;
import android.os.Parcelable;
import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.Global;
import com.yylending.las.common.AESEntity;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

/**
 * Created by laixj on 2016/11/18.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class IdCardModel extends Model {
	public final static String TAG = IdCardModel.class.getSimpleName();

	public final static int METHOD_SAVE_ID_CARD = 0;

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		switch (functionalCode) {
			case METHOD_SAVE_ID_CARD:{
				Service service = (Service) NetworkHelper.createService(Service.class, 60);
				return service.saveIdCardInfo(Global.LOGIN_TOKEN, (AESEntity) params);}
		}
		return null;
	}

	@Override
	public void onNext(Object entity) {
		CLog.i(TAG, "onNext: " + entity.toString());
		BaseResultEntity result = cast(entity);
		switch (result.requestCode) {
			case METHOD_SAVE_ID_CARD:
				SaveIdCardResult saveIdCard = cast(entity);
				EventAgent.post(saveIdCard);
				break;
		}
	}

	private interface Service {
		/**
		 * 身份证信息录入接口
		 */
		@POST("server/customer/saveIndenInfo")
		Observable<SaveIdCardResult> saveIdCardInfo(@Header("token") String token, @Body AESEntity params);
	}

	static class SaveIdCardResult extends BaseResultEntity<String> {
	}

	public static class IdCard implements Parcelable{

		public String inumber;
		public String iname;
		public String ioffice;
		public String istart;
		public String iend;
		public String iavatar;
		public String iaddress;
		public String iregion;
		public String isex;
		public String ipicFront;
		public String ipicBack;

		@Override
		public String toString() {
			return "IdCard{" +
					"inumber='" + inumber + '\'' +
					", iname='" + iname + '\'' +
					", ioffice='" + ioffice + '\'' +
					", istart='" + istart + '\'' +
					", iend='" + iend + '\'' +
					", iaddress='" + iaddress + '\'' +
					", iregion='" + iregion + '\'' +
					", isex='" + isex + '\'' +
					'}';
		}

		public IdCard() {
		}

		@Override
		public int describeContents() {
			return 0;
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeString(this.inumber);
			dest.writeString(this.iname);
			dest.writeString(this.ioffice);
			dest.writeString(this.istart);
			dest.writeString(this.iend);
			dest.writeString(this.iavatar);
			dest.writeString(this.iaddress);
			dest.writeString(this.iregion);
			dest.writeString(this.isex);
			dest.writeString(this.ipicFront);
			dest.writeString(this.ipicBack);
		}

		protected IdCard(Parcel in) {
			this.inumber = in.readString();
			this.iname = in.readString();
			this.ioffice = in.readString();
			this.istart = in.readString();
			this.iend = in.readString();
			this.iavatar = in.readString();
			this.iaddress = in.readString();
			this.iregion = in.readString();
			this.isex = in.readString();
			this.ipicFront = in.readString();
			this.ipicBack = in.readString();
		}

		public static final Creator<IdCard> CREATOR = new Creator<IdCard>() {
			@Override
			public IdCard createFromParcel(Parcel source) {
				return new IdCard(source);
			}

			@Override
			public IdCard[] newArray(int size) {
				return new IdCard[size];
			}
		};
	}
}
